
# s= serial.Serial()
# s.port='com1'
# s.baudrate='9600'
# s.open()
# s.bytesize=8
# s.parity='N'


import serial
import serial.tools.list_ports



#枚举串口列表,同时打开关闭串口一次,
#['COM1', 'ELTIMA Virtual Serial Port (COM1->COM2)', 'VSBC\\DEVICES\\0000', 'False']
def UartList(name=True,fun=False,check=True):
    port_list = list(serial.tools.list_ports.comports())# is list object ,every one is a uart
    port_num=len(port_list)
    if port_num <= 0:
        return 0
    else:
        port_list_0_value = list(port_list[0])
        attribute_num=len(port_list_0_value)

        ret = [[1 for x in range(attribute_num+1)] for y in range(port_num)]
        for i in range(port_num):
            t= list(port_list[i])
            try:
                if(check==True or check=='True'):
                    try:
                        ser = serial.Serial(list(port_list[i])[0], 9600, timeout=60)
                        #print('hello')
                        t.append('OK')
                    except:
                        t.append('ON')
                        #print('funk1')
                else:
                    t.append('ON')
                    #print('funk2')
            except:
                t.append('NONE')
                #print('funk3')
            ret[i]=t
        ret.sort()

        del_fun=2
        if(name==False):
            del_fun-=1
            for i in range(len(ret)):
                del ret[i][1]
        if(fun==False):
            for i in range(len(ret)):
                del ret[i][del_fun]
        return  ret




if __name__ == '__main__':
    a = UartList()
    a.insert(0,[1,2,3,4])
    #print([a[i] for i in range(len(a))])
    for i in range(len(a)):
        print(a[i])



